package cn.csxw.openchina.ui.activity;

import android.content.Intent;

import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Build;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

import butterknife.BindView;

import cn.csxw.openchina.R;
import cn.csxw.openchina.base.BaseActivity;
import cn.csxw.openchina.utils.Global;

/**
 * Delegate接口是处理扫描结果的一个类，有失败和成功2种结果，
 * 实现这个接口可以进行扫描结果的处理，
 * 跟网络访问的回调一样
 */
public class ScanActivity1 extends BaseActivity {
    public static final int SCAN_CODE = 1;//二维码的请求状态
    @BindView(R.id.scan_button)
    Button mScanButton;
    @BindView(R.id.scan_result)
    TextView mScanResult;

    @Override
    public int getLayoutRes() {
        return R.layout.activity_scan_zxing;
    }

    @Override
    public void onActivityResult(int requestCode,
                                 int resultCode, Intent data) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        IntentResult intentResult =
                IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (intentResult != null) {
            String result = intentResult.getContents();
            mScanResult.setText(result);
            Global.showToast("扫描成功");
        } else {
            mScanResult.setText("扫描失败");
        }
    }


    @Override
    public void initView() {
        /*int mCurrentOrientation  = getResources().getConfiguration().orientation;
        if (mCurrentOrientation ==Configuration.ORIENTATION_PORTRAIT){
            //竖屏
            setContentView(R.layout.activity_scan_zxing);
        }*/
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {

    }


    @Override
    public void onClick(View v, int id) {
        switch (id) {
            //如果是竖排,则改为横排
            case R.id.scan_button:
               /* if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                } else*/ if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                }
                IntentIntegrator intentIntegrator =
                        new IntentIntegrator(ScanActivity1.this);
                intentIntegrator.initiateScan();

        }
    }
}
